package com.gem.eolane.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gem.eolane.config.AppConfig;
import com.gem.eolane.entity.Contactus;
import com.gem.eolane.mapper.ContactusMapper;
import com.gem.eolane.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


/**
 * @Auther: cz
 * @Date: 2019/4/10 9:21
 * @Description: 用户联系我们服务层
 */
@Service
public class ContactusService extends ServiceImpl<ContactusMapper, Contactus> {

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private AppConfig appConfig;

    @Autowired
    private WebUtils webUtils;

    SimpleMailMessage message = new SimpleMailMessage();

    //联系我们
    public String contact1(Contactus contactus) throws UnsupportedEncodingException {
        System.out.println("姓1 = [" + contactus + "]");
        String titleStr = "联系我们";
        String msgStr = contactus.getSurname() +contactus.getMrs()
                +"\n个人信息"
                + "\n姓：" + contactus.getSurname()
                + "\n名：" + contactus.getName()
                + "\n电话：" + contactus.getTelephone()
                + "\n邮箱：" + contactus.getEmail()
                /*  +"\n联系地址"*/
                + "\n公司名称：" + contactus.getCompany()
                + "\n网址：" + contactus.getWebsite()
                + "\n地址：" + contactus.getAddress()
                +"\n留言：" + contactus.getMessage();
        sentMailUtils(titleStr,msgStr);
        return "101";
    }

    //我要入住
    public String contact2(Contactus contactus) throws UnsupportedEncodingException {
        System.out.println("姓2 = [" + contactus + "]");
        String titleStr = "我要入住";
        String msgStr =  "个人信息"
                + "\n姓名：" + contactus.getName()
                + "\n电话：" + contactus.getTelephone()
                + "\n邮箱：" + contactus.getEmail()
                +"\n工作要求：" + contactus.getMessage();
        sentMailUtils(titleStr,msgStr);
        return "102";
    }

    //氛围灯合作
    public String contact3(Contactus contactus) throws UnsupportedEncodingException {
        System.out.println("姓3 = [" + contactus + "]");
        String titleStr = "氛围灯合作";
        String msgStr =  "个人信息"
                + "\n姓名：" + contactus.getName()
                + "\n公司名称：" + contactus.getCompany()
                +"\n联系电话：" + contactus.getTelephone()
                + "\n邮箱：" + contactus.getEmail();
        sentMailUtils(titleStr,msgStr);
        return "103";
    }

    //ODM字段联系
    public String contact4(Contactus contactus) throws UnsupportedEncodingException {
        System.out.println("姓4 = [" + contactus + "]");
        String titleStr = "字段联系";
        String msgStr =  "个人信息"
                + "\n姓名：" + contactus.getName()
                +"\n电话：" + contactus.getTelephone()
                + "\n公司：" + contactus.getCompany();
        sentMailUtils(titleStr,msgStr);
        return "104";
    }

    //发邮箱和微信的公共部分
    public void sentMailUtils(String titleStr,String msgStr) throws UnsupportedEncodingException {
        String msg = URLEncoder.encode(msgStr,"utf8");
        String title = URLEncoder.encode(titleStr,"utf8");
       /* webUtils.sendPost("http://ebpm.eolane.cn/API/SendWeixin",
                "TagName=3&Title="+title+"&Msg="+msg+"&App=equote&VeriCode=gemEqu0te%26");*/
        message.setFrom(appConfig.mail);
        message.setTo(appConfig.mail2);
        message.setSubject(titleStr);
        message.setText(msgStr);
        mailSender.send(message);
    }
}
